/*    
 * Copyright (c) 2013 ISoftStone, Inc.
 * All right reserved.
 *
 * 文件名：      BillingListDaoPlusImpl.java
 * 作者:     Jacky Wang
 * 创建日期： 2013-9-18 下午04:36:22
 * 版本：           
 *
 */
package com.isoftstone.industry.aaa.core.dao.assist.impl;

import java.sql.Timestamp;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.isoftstone.industry.persistance.bean.AccountItem;
import com.isoftstone.industry.persistance.bean.BillingCycle;
import com.isoftstone.industry.persistance.bean.BillingList;
import com.isoftstone.industry.persistance.bean.ChargingItemDetail;
import com.isoftstone.industry.aaa.core.dao.assist.BillingListDaoPlus;
import com.isoftstone.modules.core.utils.DateUtils;

/**
 * @author jacky
 */
public class BillingListDaoImpl implements BillingListDaoPlus
{
	@PersistenceContext 
	private EntityManager em; 

	@Override
	public List<BillingList> getGroupListByUser(String userId, BillingCycle billingCycle)
	{
		String hql = "from BillingList x where x.user.id=?1 and x.accountItem is null and (x.createdTime<=?2 or (x.startTime is not null and x.endTime is not null))";
		Timestamp endTime = DateUtils.createEndTime(billingCycle.getEndDate());
		Query query = em.createQuery(hql);
		query.setParameter(1, userId);
		query.setParameter(2, endTime);
		return query.getResultList();
	}

	@Override
	public void update4AcctItem(String[] ids, AccountItem acctItem)
	{
		StringBuffer hql = new StringBuffer("update BillingList x set x.accountItem.id=?1 where (x.id=?2");
		for (int i = 1; i < ids.length; i++)
		{
			hql.append(" or ");
			hql.append("id=?");
			hql.append(2+i);
		}
		hql.append(")");
		Query query = em.createQuery(hql.toString());
		query.setParameter(1, acctItem.getId());
		for (int i = 0; i < ids.length; i++)
		{
			query.setParameter(i+2, ids[i]);
		}
		query.executeUpdate();
	}

	@Override
	public List<BillingList> getNeedPrepaidItems(int start, int limit)
	{
		String hql = "from BillingList x where x.accountItem is null and (x.type=?1 or x.type=?2 or x.type=?3)  and x.chargingItemDetail.status = ?4";
		Query query = em.createQuery(hql);
		query.setParameter(1, BillingList.Type.PRE_PAID_NONRECURRING);
		query.setParameter(2, BillingList.Type.PRE_PAID_PERIODIC);
		query.setParameter(3, BillingList.Type.PRE_USAGE);
		query.setParameter(4, ChargingItemDetail.Status.VALID);
		if (start >= 0 && limit > 0)
		{
			query.setFirstResult(start);
			query.setMaxResults(limit);
		}
		return query.getResultList();
	}

	@Override
	public long countNeedPrepaidItems()
	{
		String hql = "select count(*) from BillingList x where x.accountItem is null and (x.type=?1 or x.type=?2 or x.type=?3) and x.chargingItemDetail.status = ?4";
		Query query = em.createQuery(hql);
		query.setParameter(1, BillingList.Type.PRE_PAID_NONRECURRING);
		query.setParameter(2, BillingList.Type.PRE_PAID_PERIODIC);
		query.setParameter(3, BillingList.Type.PRE_USAGE);
		query.setParameter(4, ChargingItemDetail.Status.VALID);
		return ((Long)query.getSingleResult()).longValue();
	}

}
